use data.dta, clear

*** Rename data for easy interpretation

*** Setting up data

gen union = 2 - q62
ren q38 edu
recode edu (20 = . )

*** favorability
rename china1_1 cn1_us /* note: higher valuse = dislike */
rename china1_2 cn1_cn
rename china1_3 cn1_aus
rename china1_4 cn1_ger
rename china1_5 cn1_fr
rename china1_6 cn1_jp
rename china1_7 cn1_ind
rename china1_8 cn1_nor
rename china1_9 cn1_rus



*** Chinese military
rename china5 cn_fear
replace cn_fear = . if cn_fear==6 /* Higher values = no threat */

*** brexit questions
rename brexit1_2 brex_id /* higher values = less support */
gen nationalid = 6 - brex_id /* higher value = Leaver */

*** brexit district

// Brexit Geography Results
/* Anti-Brexit are those districts that had <50% support Leave
   Slightly Pro-Brexit are those areas that had >50% but <54% support Leave
   Strongly Pro-Brexit are those areas that had >55% support Leave */

gen geo_brexit = 2 if q36==1 /* North East */
replace geo_brexit = 1 if q36==2 /* North West */ 
replace geo_brexit = 1 if q36==3 /* Yorkshire & Humber */
replace geo_brexit = 2 if q36==4 /* East Midlands */ 
replace geo_brexit = 2 if q36==5 /* West Midlands */
replace geo_brexit = 2 if q36==6 /* East of England */
replace geo_brexit = 0 if q36==7 /* London */
replace geo_brexit = 1 if q36==8 /* South East */
replace geo_brexit = 1 if q36==9 /* South West */
replace geo_brexit = 1 if q36==10 /* Wales */
replace geo_brexit = 0 if q36==11 /* Scotland */
replace geo_brexit = 0 if q36==12 /* Northern Ireland */


*** label variables

label variable age "Age"
label variable nationalid "Brexit Identities"
label variable female "Female"
label variable income "Income"
label variable conservative "Conservative"
label variable labour "Labour"
label variable media "Media Attentiveness"
label variable geo_brexit "Pro-Brexit District"
label variable edu "Education"
label variable union "Labour Union"
label variable race_white "White"


**Replicating the figures
**Note: the actual figures in the paper are further formatted for presentation


**************
** Figure 1 **
**************


** views of China


gen favor_cn = 1 if cn1_cn == 1 | cn1_cn == 2
replace favor_cn = 2 if cn1_cn == 3
replace favor_cn = 3 if cn1_cn == 4 | cn1_cn==5

proportion favor_cn
estimates store fcn

coefplot fcn, format(%9.0f) ///
	title("(a) Overall Perception of China", size(medium)) ///
	color(teal) byopts(cols(1)) ///
	ciopts(recast(rcap)) citop citype(logit) ///	
	recast(bar) rescale(100) vertical  ///
	ytitle ("") ///
	graphregion(color(white)) yscale(r(0 50)) ylabel(0(10)50) ///
	addplot(scatter @b @at, ms(i) mlabel(@b) mlabpos(2) mlabcolor(black)) ///	
	xlabel(1 "Favourable" 2 "Neutral" 3 "Unfavourable") ///
	ylabel(, labcol(black)) barwidth(.5) 
graph save "fig1(a).gph", replace


**Policy Priority

preserve 
keep if china3!=7
proportion china3
estimates store policy
restore

coefplot policy, ///
	title("(b) Policy Priority with China for the UK", ///
	color(black) size(medium)) format(%5.0f) ///
	recast(bar) rescale(100) barwidth(.5) vertical color(teal) ///
	ytitle("", height(4)) ///
	yscale(r(0 45))	ciopts(recast(rcap)) citop citype(logit) ///
	keep(china3:1 china3:2 china3:3 china3:4 china3:5 china3:6) ///
	graphregion(color(white)) ///
	xlabel(1 `""Global" "Issues" "' 2 `""Trade" "Investment" "' 3 `""Cyber" "Security" "' ///
	4 `""Human" "Rights" "' 5 `""British" "Values" "' ///
	6 `""Intellectual" "Property" "') ///
	legend(off) ///
	addplot(scatter @b @at, ms(i) mlabel(@b) mlabpos(2) mlabcolor(black)) 
graph save "fig1(b).gph", replace


**Chinese military

gen cn_military = 1 if cn_fear==1 | cn_fear==2
replace cn_military = 3 if cn_fear==4 | cn_fear==5
replace cn_military = 2 if cn_fear==3

proportion cn_military
estimates store military

coefplot military, format(%9.0f) ///
	title("(c) Rise of Chinese Military Power a Concern?", ///
	color(black) size(medium)) byopts(cols(1)) ///
	ciopts(recast(rcap)) citop citype(logit) ///	
	recast(bar) rescale(100) vertical color(teal) ///
	ytitle ("") ///
	graphregion(color(white)) yscale(r(0 50)) ylabel(0(10)50) ///
	addplot(scatter @b @at, ms(i) mlabel(@b) mlabpos(2) mlabcolor(black)) ///	
	xlabel(1 "Agree" 3 "Disagree" ///
	2 `""Neither agree" "nor disagree" "', labcol(black)) ///
	ylabel(, labcol(black)) barwidth(.5)
graph save "fig1(c).gph", replace

	
**trade agreement

gen trade_eu = 6 - china4_1
gen trade_us = 6 - china4_2
gen trade_cn = 6 - china4_3

	
// gen trade variables
replace trade_eu = china4_1==1 | china4_1==2		
replace trade_cn = china4_3==1 | china4_3==2		
replace trade_us = china4_2==1 | china4_2==2		

// EU trade
proportion trade_eu
estimates store teu

// US Trade
proportion trade_us
estimates store tus

// China Trade
proportion trade_cn
estimates store tcn

coefplot teu, bylabel("(a) European Union") ///
	|| tus, bylabel("(b) United States") ///
	|| tcn, bylabel("(c) China") format(%9.0f) ///
	title("", color(black)) byopts(rows(1)) ///
	recast(bar) rescale(100) vertical color(teal) ///
	yscale(r(0(20)100)) ///
	ciopts(recast(rcap)) citop citype(logit) ///	
	ytitle("") ///
	addplot(scatter @b @at, ms(i) mlabel(@b) mlabpos(2) mlabcolor(black)) ///
	barwidth(.5) 	xlabel(1 "Low" 2 "High")
graph save "fig1(d).gph", replace


graph combine "fig1(a).gph" "fig1(b).gph" "fig1(c).gph" "fig1(d).gph"	


**************
** Figure 2 **
**************

** views of China by Brexit identities
** Note: the three responses are in reversed order in Figure 2 in the paper.  

proportion favor_cn if nationalid==1
estimates store fcn1

proportion favor_cn if nationalid==5
estimates store fcn5

coefplot fcn1 fcn5, format(%9.0f) ///
	title("(a) Overall Perception of China", size(medium)) ///
	color(teal) byopts(cols(1)) ///
	ciopts(recast(rcap)) citop citype(logit) ///	
	recast(bar) rescale(80) vertical  ///
	ytitle ("") ///
	graphregion(color(white)) yscale(r(0 50)) ylabel(0(10)50) ///
	addplot(scatter @b @at, ms(i) mlabel(@b) mlabpos(2) mlabcolor(black)) ///	
	xlabel(1 "Favourable" 2 "Neutral" 3 "Unfavourable") ///
	ylabel(, labcol(black)) barwidth(.3) legend(label(1 "Remainers") label(3 "Leavers") ring(0) position(2) bmargin(large))
graph save "fig2(a).gph", replace


**Chinese military by Brexit identities


proportion cn_military if nationalid==1
estimates store military1

proportion cn_military if nationalid==5
estimates store military5

coefplot military1 military5, format(%9.0f) ///
	title("(b) Rise of Chinese Military Power a Concern?", ///
	color(black) size(medium)) byopts(cols(1)) ///
	ciopts(recast(rcap)) citop citype(logit) ///	
	recast(bar) rescale(100) vertical color(teal) ///
	ytitle ("") ///
	graphregion(color(white)) yscale(r(0 60)) ylabel(0(10)60) ///
	addplot(scatter @b @at, ms(i) mlabel(@b) mlabpos(2) mlabcolor(black)) ///	
	xlabel(1 "Agree" 3 "Disagree" ///
	2 `""Neither agree" "nor disagree" "', labcol(black)) ///
	ylabel(, labcol(black)) barwidth(.3) legend(label(1 "Remainers") label(3 "Leavers") ring(0) position(2) bmargin(large))
graph save "fig2(b).gph", replace

graph combine "fig2(a).gph" "fig2(b).gph"
	
	
**************
** Figure 3 **
**************

**FTA by Brexit identities


gen trade_eu = 6 - china4_1
gen trade_us = 6 - china4_2
gen trade_cn = 6 - china4_3

// gen trade variables
replace trade_eu = china4_1==1 | china4_1==2		
replace trade_cn = china4_3==1 | china4_3==2		
replace trade_us = china4_2==1 | china4_2==2		

// EU trade
proportion trade_eu if nationalid==1
estimates store teu1

proportion trade_eu if nationalid==5
estimates store teu5

// US Trade
proportion trade_us if nationalid==1
estimates store tus1

proportion trade_us if nationalid==5
estimates store tus5

// China Trade
proportion trade_cn if nationalid==1
estimates store tcn1

proportion trade_cn if nationalid==5
estimates store tcn5

coefplot teu1 teu5, bylabel("(a) European Union") ///
	|| tus1 tus5, bylabel("(b) United States") ///
	|| tcn1 tcn5, bylabel("(c) China") format(%9.0f) ///
	title("(d) Priority for Free Trade Agreement with ...", color(black)) byopts(rows(1)) ///
	recast(bar) rescale(100) vertical color(teal) ///
	yscale(r(0(20)100)) ///
	ciopts(recast(rcap)) citop citype(logit) ///	
	ytitle("") yscale(r(0 100)) ylabel(0(20)100) ///
	addplot(scatter @b @at, ms(i) mlabel(@b) mlabpos(2) mlabcolor(black)) ///
	barwidth(.3) legend(label(1 "Remainers") label(3 "Leavers") col(2)) /// 
	xlabel(1 "Low" 2 "High")
graph save "fig3.gph", replace


**************
** Figure 4 **
**************

** policy priority by Brexit identities

preserve 
keep if china3!=7
proportion china3 if nationalid==1
estimates store policy1
proportion china3 if nationalid==5
estimates store policy5
restore

coefplot policy1 policy5, ///
	title("Policy Priority with China for the UK", ///
	color(black) size(medium)) format(%5.0f) ///
	recast(bar) rescale(100) barwidth(.5) vertical color(teal) ///
	ytitle("", height(4)) ///
	yscale(r(0 45))	ciopts(recast(rcap)) citop citype(logit) ///
	keep(china3:1 china3:2 china3:4) ///
	graphregion(color(white)) ///
	xlabel(1 "Global Issues" 2 "Trade Investment" 3 "Human Rights") ///
	ylabel(, labcol(black)) barwidth(.3) legend(label(1 "Remainers") label(3 "Leavers") ring(0) position(2) bmargin(large)) ///
	addplot(scatter @b @at, ms(i) mlabel(@b) mlabpos(2) mlabcolor(black)) 
graph save "fig4.gph", replace


**************
** Figure 5 **
**************
	
******** Sino-US Rivalry ********


// economic power	
proportion china2_1
estimates store mc1

// global leadership
proportion china2_2 
estimates store mc3

// global peace
proportion china2_3
estimates store mc5

// stability
proportion china2_4
estimates store mc7

// respectful of other people
proportion china2_5
estimates store mc9

// free speech
proportion china2_6 
estimates store mc11

// help the poor
proportion china2_7 
estimates store mc13

// environment
proportion china2_8 
estimates store mc15


coefplot mc1 , bylabel("(a) Larger Economy") ///
	|| mc3 , bylabel("(b) Global Leadership") ///
	|| mc5 , bylabel("(c) Global Peace") ///
	|| mc7 , bylabel("(d) Stable & Predictable") ///
	|| mc9 , bylabel("(e) Respectful of Others") ///
	|| mc11 , bylabel("(f) Free Speech") ///
	|| mc13 , bylabel("(g) Help the Poor")  ///
	|| mc15 , bylabel("(h) Environment")  ///
	title("", color(black)) format(%9.0f) ///
	recast(bar) rescale(100) vertical color(teal) ///
	ciopts(recast(rcap)) citop citype(logit) ///	
	ytitle("") byopts(rows(2)) ///
	xlabel(1 "China" 2 "US" 3 "Uncertain", labcol(black) angle(45)) ///		
	yscale(r(0 65) off) ///
	addplot(scatter @b @at, ms(i) mlabel(@b) mlabpos(2) mlabcolor(black)) ///
	barwidth(.4)
graph save "fig5.gph", replace

**************
** Figure 6 **
**************	
	
****US China comparison by Brexit identities


// economic power	
proportion china2_1 if nationalid==1
estimates store mc1

proportion china2_1 if nationalid==5
estimates store mc2

// global leadership
proportion china2_2 if nationalid==1 
estimates store mc3

proportion china2_2 if nationalid==5 
estimates store mc4

// global peace
proportion china2_3 if nationalid==1
estimates store mc5

proportion china2_3 if nationalid==5
estimates store mc6

// stability
proportion china2_4 if nationalid==1
estimates store mc7

proportion china2_4 if nationalid==5
estimates store mc8

// respectful of other people
proportion china2_5 if nationalid==1
estimates store mc9

proportion china2_5 if nationalid==5
estimates store mc10

// free speech
proportion china2_6 if nationalid==1 
estimates store mc11

proportion china2_6 if nationalid==5 
estimates store mc12

// help the poor
proportion china2_7 if nationalid==1 
estimates store mc13

proportion china2_7 if nationalid==5 
estimates store mc14

// environment
proportion china2_8 if nationalid==1 
estimates store mc15

proportion china2_8 if nationalid==5 
estimates store mc16


coefplot mc1 mc2, bylabel("(a) Larger Economy") ///
	|| mc3 mc4, bylabel("(b) Global Leadership") ///
	|| mc5 mc6, bylabel("(c) Global Peace") ///
	|| mc7 mc8, bylabel("(d) Stable & Predictable") ///
	|| mc9 mc10, bylabel("(e) Respectful of Others") ///
	|| mc11 mc12, bylabel("(f) Free Speech") ///
	|| mc13 mc14, bylabel("(g) Help the Poor")  ///
	|| mc15 mc16, bylabel("(h) Environment")  ///
	title("", color(black)) format(%9.0f) ///
	recast(bar) rescale(100) vertical color(teal) ///
	ciopts(recast(rcap)) citop citype(logit) ///	
	ytitle("") byopts(rows(2)) ///
	xlabel(1 "China" 2 "US" 3 "Uncertain", labcol(black) angle(45)) ///		
	yscale(r(0 65) off) ///
	addplot(scatter @b @at, ms(i) mlabel(@b) mlabpos(2) mlabcolor(black)) ///
	barwidth(.3) legend(label(1 "Remainers") label(3 "Leavers") col(2)) 
graph save "fig6.gph", replace
